import request from '@/utils/request'
import requestLong from '@/utils/requestLong'
import common from './common'

const api = {
  getMaterialList: 'projectMaterial/list/{projectId}', // 获取材料
  addDirectory: 'projectMaterial/project/{projectId}/material/directory/create', // 材料添加目录
  upload: 'projectMaterial/project/{projectId}/material/upload', // 上传
  download: 'materialInst/file/download',
  downPackage: 'projectMaterial/project/{projectId}/material/package',
  deleteMaterial: 'projectMaterial/file/delete',
  viewLogMaterial: 'projectMaterial/file/viewLog',
  deleteMaterialDir: 'projectMaterial/project/{projectId}/material/directory/{dirInstId}/delete', // 删除目录
  editFileName: 'materialInst/project/{projectId}/material/{materialId}/save', // 编辑文件名称
  editDirName: 'projectMaterial/project/{projectId}/material/directory/{dirInstId}/update', // 编辑目录名称
  getProjectMaterialList: 'projectMaterial/getProjectMaterialList', // 获取材料袋列表
  uploadCustomFile: 'materialInst/material/uploadCustomFile', // 上传自定义材料
  deleteCustomFile: 'materialInst/file/delete', // 删除自定义材料
  uploadAnalyseFile: 'app/gis/common/uploadAnalyseFile' // 上传图形审查分析结果
}

const fix = window.config.VUE_APP_BASE_URL.endsWith('/') ? '' : '/'

export default {
  uploadAnalyseFile (params) {
    return request({
      url: api.uploadAnalyseFile,
      method: 'post',
      params: params
    })
  },
  getMaterialList (params) {
    return request({
      url: api.getMaterialList.replace(/\{projectId\}/, params.projectId),
      method: 'get',
      params: params
    })
  },
  deleteMaterial (params, data) {
    return common.postxf(api.deleteMaterial, params)
  },
  deleteCustomFile (params, data) {
    return common.postxf(api.deleteCustomFile, params)
  },
  viewLogMaterial (params, data) {
    return common.postxf(api.viewLogMaterial, params)
  },
  deleteMaterialDir (params, data) {
    return request({
      url: api.deleteMaterialDir.replace(/\{projectId\}/, params.projectId).replace(/\{dirInstId\}/, params.dirInstId),
      method: 'post',
      params: params,
      data
    })
  },
  // 材料添加目录
  addDirectory (params, data) {
    return request({
      url: api.addDirectory.replace(/\{projectId\}/, params.projectId),
      method: 'post',
      params: params,
      data
    })
  },
  // 编辑文件名称
  editFileName (params, data) {
    return request({
      url: api.editFileName,
      method: 'post',
      params: params,
      data
    })
  },
  // 编辑目录名称
  editDirName (params, data) {
    return request({
      url: api.editDirName.replace(/\{projectId\}/, params.projectId).replace(/\{dirInstId\}/, params.dirInstId),
      method: 'post',
      params: params,
      data
    })
  },
  downloadMaterial (params) {
    let vars = '?'
    for (var key in params) {
      vars += key + '=' + params[key] + '&'
    }
    vars = vars.substring(0, vars.length - 1)
    const url = window.config.VUE_APP_BASE_URL + fix + api.download + vars
    const a = document.createElement('a')
    a.href = url
    a.click()
  },
  downloadPDF (url) {
    const oReq = new XMLHttpRequest()
    const URL = url
    oReq.open('GET', URL, true)
    oReq.responseType = 'blob'
    oReq.onload = function (res) {
      const file = new Blob([oReq.response], {
        type: 'blob'
      })
      const a = document.createElement('a')
      const URL = window.URL || window.webkitURL
      const link = URL.createObjectURL(file)
      a.href = link
      a.download = '操作手册.pdf'
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
      window.URL.revokeObjectURL(link)
    }
    oReq.send()
  },
  downPackage (params) {
    let vars = '?'
    for (var key in params) {
      vars += key + '=' + params[key] + '&'
    }
    vars = vars.substring(0, vars.length - 1)
    const url = window.config.VUE_APP_BASE_URL + fix + api.downPackage.replace(/\{projectId\}/, params.projectId) + vars
    const a = document.createElement('a')
    a.href = url
    a.click()
  },
  getProjectMaterialList (params) {
    return request({
      url: api.getProjectMaterialList,
      method: 'get',
      params: params
    })
  }
}

export function upload (params, data, callback) {
  return requestLong({
    url: api.upload.replace(/\{projectId\}/, params.projectId),
    method: 'post',
    params: params,
    data: data,
    onUploadProgress (progressEvent) {
      if (progressEvent.lengthComputable) {
        const val = Number((progressEvent.loaded / progressEvent.total * 100).toFixed(2))
        callback(parseInt(val))
      }
    }
  })
}

export function uploadCustomFile (params, data, callback) {
  return requestLong({
    url: api.uploadCustomFile,
    method: 'post',
    params: params,
    data: data,
    onUploadProgress (progressEvent) {
      if (progressEvent.lengthComputable) {
        const val = Number((progressEvent.loaded / progressEvent.total * 100).toFixed(2))
        callback(parseInt(val))
      }
    }
  })
}
